package com.yuanqi.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuanqi.Common.config.ThreadLocalConfig;
import com.yuanqi.Common.Pojo.entity.Feedback;
import com.yuanqi.Return.Result;
import com.yuanqi.Service.FeedBaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/v1/feedback")
public class FeedBackController {
    @Resource
    FeedBaskService feedBaskService;
    @Resource
    ThreadLocalConfig threadLocalConfig;

    /**
     * 反馈
     * @param feedback
     * @return
     */
    @PostMapping
    public Result addFeedBack(@RequestBody Feedback feedback){
        Long userId = Long.valueOf(threadLocalConfig.get().get("userId").toString());
        feedback.setFromId(userId);
//        这是客服的Id,现在默认是一个客服
        feedback.setToId(0);
        feedback.setCreateTime(System.currentTimeMillis()/1000);
        feedBaskService.save(feedback);
        return Result.success();
    }

    /**
     * 获取反馈列表
     * @return
     */

    @GetMapping("/SelectFeedBack")
    public Result SelectFeedBack(){
        Long userId = Long.valueOf(threadLocalConfig.get().get("userId").toString());
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Feedback::getFromId,userId);
        wrapper.orderByDesc(Feedback::getCreateTime);
        wrapper.eq(Feedback::getToId,0);
        List<Feedback> list = feedBaskService.list(wrapper);
        return Result.success(list);
    }
}
